define([
    'jquery',
    'butterfly/view',
    'underscore',
    'backbone',
    'butterflysoc/components/list',
    'butterflysoc/components/session',
    'butterflysoc/components/base64Img'
],
    function ($, View, _, Backbone, List, 
                           Session,
                           Base64Img
                          
        ) {

        // Cached regex to split keys for `delegate`.
        var delegateEventSplitter = /^(\S+)\s*(.*)$/;
//        var cubeView = Backbone.View.extend({
        var cubeView = View.extend({
            className: 'page',
            components: {},

            compile: function () {
                var me = this;

 
               var lists = List.compile(this.el);
                var base64img = Base64Img.compile(this.el);
                var result = _.union(lists);
//                var result = _.union(lists, extendableLists);
                _.each(result, function (component) {
                    var id = component.id || component.el.getAttribute('id');
                    me.components[id] = component;
                });


                //back button intercept
                this.$el.find('.back').click(function() {
                    var hrefDisable = $(this).attr("hrefDisable");
                    if (hrefDisable != null && hrefDisable == "cube://exit") {
                        window.location.href="cube://exit";
                    } else {
                        if($(this).attr("data-navigate") && $(this).attr("data-navigate") === "false") {
                            return;
                        }else{
                            window.history.back();
                            me.refreshSession();
                        }
                        
                    }
                    return false;
                });

            },
            refreshSession: function() {

            },
            bindEvents: function () {
                if (!this.bindings) return;

                for (var key in this.bindings) {
                    var method = this.bindings[key];
                    if (!_.isFunction(method)) method = this[method];
                    if (!method) throw new Error('Method "' + this.bindings[key] + '" does not exist');
                    var match = key.match(delegateEventSplitter);
                    var eventName = match[1],
                        componentId = match[2];
                    method = _.bind(method, this);
                    var comp = this.component(componentId);
                    if (comp) comp.on(eventName, method, this);
                }
            },
            unbindEvents: function () {
                for (var key in this.bindings) {
                    var match = key.match(delegateEventSplitter);
                    var eventName = match[1],
                        componentId = match[2];
                    var comp = this.component(componentId);
                    if (comp) comp.off(eventName);
                }
            },

            component: function (id) {
                return this.components[id];
            },

            initialize: function () {
                var objectString = window.localStorage['stepForward'];
                objectString == null ? null : JSON.parse(objectString);
                window.localStorage['stepForward'] = null;
                window.localStorage['lastUrl'] = window.localStorage['currentUrl'];
                if (objectString == 1) {
                    this.stepForward = 1;
                } else {
                    if (window.localStorage['currentUrl'] === window.location.href) {
                        this.stepForward = 0;
                    } else {
                        this.stepForward = -1;
                    }
                }
                window.localStorage['currentUrl'] = window.location.href;
                Backbone.View.prototype.initialize.call(this);
            },

            remove: function () {
                $(window).off('orientationchange', this.onOrientationchange);
                this.unbindEvents();
                Backbone.View.prototype.remove.call(this);
            },

            render: function () {
                var self = this;
//                $(document).bind('show', function () {
//                    self.onShow();
//                });
                this.compile();
                this.unbindEvents();
                this.bindEvents();
                return this;
            }

        });

        return cubeView;
    });